package com.hns.can.master.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysRoleEntity;
import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.CommCarCurrentLoInfoEntity;
import com.hns.can.master.service.BaseOrganService;
import com.hns.can.master.service.HomeFactoryWholeService;
import com.hns.can.master.vo.HomeFactoryFaultRankVo;
import com.hns.can.master.vo.HomeFactoryIndexVo;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.SearchConditionVo;

@Controller
@RequestMapping("/homeFactory")
public class HomeFactoryWholeController extends BaseController {
	@Autowired
	private HomeFactoryWholeService homeFactoryWholeService;
	@Autowired
	private BaseOrganService baseOrganService;
	@Autowired
	private CommonInitParamService commonInitParamService;

	@RequestMapping("/getTotalIndex")
	@ResponseBody
	public Object getTotalIndex(){
		SysUserEntity user = this.getCurrentUser().getUser();
		HashMap map = new HashMap();
		map.put("userId", user.getId());
		HomeFactoryIndexVo index = homeFactoryWholeService.getTotalIndex(map);
		return this.getReturnResult(index);
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/getFaultRanking")
	@ResponseBody
	public Object getFaultRanking(){
		SysUserEntity user = this.getCurrentUser().getUser();
		HashMap map = new HashMap();
		SearchConditionVo param = new SearchConditionVo();
		commonInitParamService.dateType2DateTime(param, "01");
		map.put("userId", user.getId());
		map.put("beginTime", param.getBeginTime());
		map.put("endTime", param.getEndTime());
		map.put("corpId", user.getCorpId());
		List<SysRoleEntity> sysRoleList = new ArrayList(this.getCurrentUser().getSet_role());
		map.put("roleIdList", sysRoleList);
		List<HomeFactoryFaultRankVo> list = homeFactoryWholeService.getFaultRanking(map);
		return this.getReturnResult(list);
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/getAreaCar")
	@ResponseBody
	public Object getAreaCar(){
		Set<SysRoleEntity> role = this.getCurrentUser().getSet_role();
		List<SysRoleEntity> roleList = new ArrayList<SysRoleEntity>(role);
		List list = homeFactoryWholeService.getAreaCar(roleList, this.getCurrentUser().getUser().getId());
		return this.getReturnResult(list);
	}
	
	/**
     * @param map
     * @param request
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     * @创建日期:2015-1-9
     * @功能说明：获取车的最终位置
     */
    @RequestMapping("/getCarPositionList")
    @ResponseBody
    public Object getCarPositionList(String province, String city, String licPltNo) throws Exception {
        HashMap params = new HashMap();
        params.put("province", province);
        params.put("city", city);
        List<SysRoleEntity> sysRoleList = new ArrayList(this.getCurrentUser().getSet_role());
        params.put("roleIdList", sysRoleList);
        if(PubUtil.isNotEmpty(licPltNo)){
        	params.put("licPltNo", licPltNo.replace("'", ""));
        }
        // 获取当前数据
        List<CommCarCurrentLoInfoEntity> list = homeFactoryWholeService.getCarPositionList(params);
        return this.getReturnResult(list);
    }
	
    @RequestMapping("/getProvinceLocation")
    @ResponseBody
    public Object getProvinceLocation(HttpServletRequest request, HttpServletResponse response){
    	Set<SysRoleEntity> role = this.getCurrentUser().getSet_role();
		List<SysRoleEntity> roleList = new ArrayList<SysRoleEntity>(role);
    	List areaCarCount = homeFactoryWholeService.getProvinceLocation(this.getCurrentUser().getUser().getCorpId(), roleList, this.getCurrentUser().getUser().getId());
    	return this.getReturnResult(areaCarCount);
    }
    
    @RequestMapping("/getCityLocation")
    @ResponseBody
    public Object getCityLocation(HttpServletRequest request, HttpServletResponse response){
    	Set<SysRoleEntity> role = this.getCurrentUser().getSet_role();
		List<SysRoleEntity> roleList = new ArrayList<SysRoleEntity>(role);
    	List areaCarCount = homeFactoryWholeService.getCityLocation(this.getCurrentUser().getUser().getCorpId(), roleList, this.getCurrentUser().getUser().getId());
    	return this.getReturnResult(areaCarCount);
    }
    
}
